﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace WinService
{
    public partial class ReminderMail : ServiceBase
    {
        private Timer tm;
        public ReminderMail()
        {
            InitializeComponent();
            tm = new Timer();
            tm.Interval = Convert.ToInt32(ConfigurationSettings.AppSettings["interval"].ToString());
            tm.Elapsed +=  new System.Timers.ElapsedEventHandler(tm_Elapsed);
            if (!System.Diagnostics.EventLog.SourceExists("ReminderMailSource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "ReminderMailSource", "ReminderMailLog");
            }
            eventLog1.Source = "ReminderMailSource";
            eventLog1.Log = "ReminderMailLog";
        }

        void tm_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                eventLog1.WriteEntry("Sending Mail");
                MailReminderUser rm = new MailReminderUser();
                rm.MailReminderExpense();
                rm.MailReminderToUser();
                eventLog1.WriteEntry("Mail Send");
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Error : "+ ex.Message);
               
            }
           
        }

        protected override void OnStart(string[] args)
        {
          
            tm.Start();
            eventLog1.WriteEntry("Starting Service");
          
        }

        protected override void OnStop()
        {
            tm.Stop();
            eventLog1.WriteEntry("Stopping Service");
        }
    }
}
